package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblDKMHKetqua;
import com.lb.jwitter.service.TblDKMHKetquaService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHKetquaInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private Long dotDKId; 
    public void setDotDKId(Long dotDKId) {
       this.dotDKId = dotDKId;
 }

    public Long getDotDKId() {
       return dotDKId;
 }

    private TblDKMHDotDKService tblDKMHDotDKService; 

    private Long hocVienId; 
    public void setHocVienId(Long hocVienId) {
       this.hocVienId = hocVienId;
 }

    public Long getHocVienId() {
       return hocVienId;
 }
    
    private String MSSV; 
    public void setMSSV(String mssv) {
       this.MSSV = mssv;
 }

    public String getMSSV() {
       return MSSV;
 }

    private TblThongTinHocVienService tblThongTinHocVienService; 

    private Long nLMHId; 
    public void setNLMHId(Long nLMHId) {
       this.nLMHId = nLMHId;
 }

    public Long getNLMHId() {
       return nLMHId;
 }
    private String maMonHoc; 
    public void setMaMonHoc(String maMonHoc) {
       this.maMonHoc = maMonHoc;
 }

    public String getMaMonHoc() {
       return maMonHoc;
 }
    
    private String maNhom; 
    public void setMaNhom(String maNhom) {
       this.maNhom = maNhom;
 }

    public String getMaNhom() {
       return maNhom;
 }

    private TblThongTinNhomLopMonHocService tblThongTinNhomLopMonHocService; 

    private String ketQua; 
    public void setKetQua(String ketQua) {
       this.ketQua = ketQua;
 }

    public String getKetQua() {
       return ketQua;
 }

    private String ketQuaLyDo; 
    public void setKetQuaLyDo(String ketQuaLyDo) {
       this.ketQuaLyDo = ketQuaLyDo;
 }

    public String getKetQuaLyDo() {
       return ketQuaLyDo;
 }

    private String maDieuChinh; 
    public void setMaDieuChinh(String maDieuChinh) {
       this.maDieuChinh = maDieuChinh;
 }

    public String getMaDieuChinh() {
       return maDieuChinh;
 }

    private String ngayDangKy; 
    public void setNgayDangKy(String ngayDangKy) {
       this.ngayDangKy = ngayDangKy;
 }

    public String getNgayDangKy() {
       return ngayDangKy;
 }

    private String ngayCapNhat; 
    public void setNgayCapNhat(String ngayCapNhat) {
       this.ngayCapNhat = ngayCapNhat;
 }

    public String getNgayCapNhat() {
       return ngayCapNhat;
 }

    private String khoaMonHocYN; 
    public void setKhoaMonHocYN(String khoaMonHocYN) {
       this.khoaMonHocYN = khoaMonHocYN;
 }

    public String getKhoaMonHocYN() {
       return khoaMonHocYN;
 }

    private int xacNhanYN; 
    public void setXacNhanYN(int xacNhanYN) {
       this.xacNhanYN = xacNhanYN;
 }

    public int getXacNhanYN() {
       return xacNhanYN;
 }

    private String tinhTrang; 
    public void setTinhTrang(String tinhTrang) {
       this.tinhTrang = tinhTrang;
 }

    public String getTinhTrang() {
       return tinhTrang;
 }

    private int canBoID; 
    public void setCanBoID(int canBoID) {
       this.canBoID = canBoID;
 }

    public int getCanBoID() {
       return canBoID;
 }

    private TblCanBoService tblCanBoService; 

    private String maDieuChinh2; 
    public void setMaDieuChinh2(String maDieuChinh2) {
       this.maDieuChinh2 = maDieuChinh2;
 }

    public String getMaDieuChinh2() {
       return maDieuChinh2;
 }

    private int thuTuHangDoi; 
    public void setThuTuHangDoi(int thuTuHangDoi) {
       this.thuTuHangDoi = thuTuHangDoi;
 }

    public int getThuTuHangDoi() {
       return thuTuHangDoi;
 }

    private String ngayCapNhatCanBo; 
    public void setNgayCapNhatCanBo(String ngayCapNhatCanBo) {
       this.ngayCapNhatCanBo = ngayCapNhatCanBo;
 }

    public String getNgayCapNhatCanBo() {
       return ngayCapNhatCanBo;
 }

    private TblDKMHKetqua tblDKMHKetqua; 
    public TblDKMHKetqua  getTblDKMHKetqua() {
        return tblDKMHKetqua;
 }

    private TblDKMHKetquaService tblDKMHKetquaService; 

	private int preInsert(Message msg, TblDKMHKetqua obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblDKMHKetqua obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_Ketqua", "them")){ 
        return "noprivilege"; 
    } 
        tblDKMHKetquaService = (TblDKMHKetquaService) context.getBean("tbl_DKMH_KetquaClient"); 
        tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient"); 

        tblThongTinHocVienService = (TblThongTinHocVienService) context.getBean("tbl_ThongTin_HocVienClient"); 

        tblThongTinNhomLopMonHocService = (TblThongTinNhomLopMonHocService) context.getBean("tbl_ThongTin_NhomLop_MonHocClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 
        //-----------------------------------
        //Tim mssv
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("MSSV");
			con.setValue1(MSSV);
			con.setOperator("eq");
			cons.add(con);
		}

		List<TblThongTinHocVien> hocviens = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons, 0, 0);
		if (hocviens != null && hocviens.size() > 0) {
			hocVienId = hocviens.get(0).getId();
		} else {
			return Action.ERROR;
		}
        
        //tim nhom lop mon hoc
		cons = new ArrayList<ConditionReport>();
		
		{
			ConditionReport con = new ConditionReport();
			con.setCol("dotDKId.id");
			con.setValue1(dotDKId);
			con.setOperator("eq");
			cons.add(con);
		}
		
		{
			ConditionReport con = new ConditionReport();
			con.setCol("monHocId.ma");
			con.setValue1(maMonHoc);
			con.setOperator("eq");
			cons.add(con);
		}
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(maNhom);
			con.setOperator("eq");
			cons.add(con);
		}

		List<TblThongTinNhomLopMonHoc> nlmhs = tblThongTinNhomLopMonHocService.findtblThongTinNhomLopMonHocByCondition(cons, 0, 0);
		if (nlmhs != null && nlmhs.size() > 0) {
			nLMHId = nlmhs.get(0).getId();
		} else {
			return Action.ERROR;
		}
        
        //------------------------------------
        msg = new Message(); 
        tblDKMHKetqua = new TblDKMHKetqua(); 
        tblDKMHKetqua.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId))); 
        tblDKMHKetqua.setHocVienId(tblThongTinHocVienService.gettblThongTinHocVien(new Long(hocVienId))); 
        tblDKMHKetqua.setNLMHId(tblThongTinNhomLopMonHocService.gettblThongTinNhomLopMonHoc(new Long(nLMHId))); 
        tblDKMHKetqua.setKetQua(ketQua); 
        tblDKMHKetqua.setKetQuaLyDo(ketQuaLyDo); 
        tblDKMHKetqua.setMaDieuChinh(maDieuChinh); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayDangKy); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHKetqua.setNgayDangKy(d); 
        
       dd = df.parse(ngayCapNhat); 
       c.setTime(dd);
       d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHKetqua.setNgayCapNhat(d); 
        
        tblDKMHKetqua.setKhoaMonHocYN(khoaMonHocYN); 
        if(xacNhanYN == 1){ 
           tblDKMHKetqua.setXacNhanYN(true); 
       } else { 
           tblDKMHKetqua.setXacNhanYN(false); 
       } 
        tblDKMHKetqua.setTinhTrang(tinhTrang); 
        
        tblDKMHKetqua.setCanBoID(taiKhoanDangNhap.getCanBoId()); 
        tblDKMHKetqua.setMaDieuChinh2(maDieuChinh2); 
        tblDKMHKetqua.setThuTuHangDoi(thuTuHangDoi); 
        
        dd = new Date(); 
       c.setTime(dd);
        d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHKetqua.setNgayCapNhatCanBo(d); 
        preInsert(msg,tblDKMHKetqua); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblDKMHKetquaService.createtblDKMHKetqua(tblDKMHKetqua); 
           postInsert(msg, tblDKMHKetqua); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblDKMHKetqua.setId(id); 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&dotDKId="; 
      dataString += "&hocVienId="; 
      dataString += "&nLMHId="; 
      dataString += "&ketQua="; 
      dataString += "&ketQuaLyDo="; 
      dataString += "&maDieuChinh="; 
      dataString += "&ngayDangKy="; 
      dataString += "&ngayCapNhat="; 
      dataString += "&khoaMonHocYN="; 
      dataString += "&xacNhanYN="; 
      dataString += "&tinhTrang="; 
      dataString += "&canBoID="; 
      dataString += "&maDieuChinh2="; 
      dataString += "&thuTuHangDoi="; 
      dataString += "&ngayCapNhatCanBo="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_DKMH_Ketqua Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
